<template>
  <div class="layout-parent w100 p10">
    <el-tabs type="border-card">
      <el-tab-pane>
        <template #label><vp-label zh="基本信息" en="Basic Info"/></template>
        <VoluntaryDept :id="businessId" :type="'valid'" ref="taskRef" @dataEmits="getData"></VoluntaryDept>
        <vp-card>
          <template #header><vp-label zh="验证情况" en="Verification Status"/></template>
          <el-form ref="processFormRef" label-suffix=":" :model="formData" :rules="processFormRules"  label-width="120px" class="w100">
            <el-row class="w100">
                <vp-col :span="1" >
                  <el-form-item prop="yzyj" :rules="[validateUtils.notNull]">
                    <template #label><vp-label zh="验证情况" en="Verification Status"/></template>
                    <el-input
                          v-model="formData.yzyj"
                          maxlength="1300"
                          :autosize="{ minRows: 2}"
                          type="textarea"
                      />
                  </el-form-item>
                </vp-col>
              </el-row>
            </el-form> 
        </vp-card>
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="流程记录" en="Process Record"/></template>
        <ProcessHistory :processInsId="processInsId" />
      </el-tab-pane>
      <el-tab-pane>
        <template #label><vp-label zh="流程图" en="Process Chart"/></template>
        <ProcessView :processInsId="processInsId" />
      </el-tab-pane>
    </el-tabs>
    <StatusBar
      :id="formData.id"
      :status="formData.zt"
      :isValid="formData.yxbs"
    />
    <PageBottom>
      <VpProcessToolBar 
        :process-ins-id="processInsId" 
        :task-id="taskId" 
        :loading="submitLoading" 
        :before-show-fn="beforeShowFn"
        :after-agree-fn="afterAgreeFn"
        :set-loading-fn="(_loading: boolean) => { submitLoading = _loading }"
        :reject-last-node="true"
      />
    </PageBottom>
  </div>
</template>
<script lang="ts">
import {
  defineComponent,
  getCurrentInstance,
  onMounted,
  reactive,
  ref,
  toRefs,
} from "vue";
import request from "/@/utils/request";
import ProcessHistory from '/@/components/vp/workflow/ProcessHistory.vue';
import ProcessView from "/@/components/vp/workflow/ProcessView.vue";
import ProcessHandle from "/@/components/vp/workflow/VpProcessHandle.vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import VoluntaryDept from "./VoluntaryDept.vue";
import PageBottom from '/@/components/vp/PageBottom/index.vue';
import validateUtils from "/@/utils/validateUtils";
import mittBus from '/@/utils/mitt';
import StatusBar from '/@/components/vp/status/StatusBar.vue';
import VpProcessToolBar from '/@/components/vp/workflow/VpProcessToolBar.vue';
import { useUserInfo } from '/@/stores/userInfo';

export default defineComponent({
  name: "auditVoluntaryDept4Valid",
  props: {
    businessId: String,
    taskId: String,
    processInsId: String,
  },
  components: { ProcessHistory, ProcessView, ProcessHandle, VoluntaryDept, PageBottom, StatusBar, VpProcessToolBar},
  setup(props) {
    const state = reactive({
      loading: false,
      submitLoading: false,
      type: "audit",
      formData: {
        id: "",
        zt: 10,
        yxbs: 1,
        jb: null,
        yzyj: "",
        processHandle: {},
      },
      jb: 10,
      processExtFormData: {
        sfnrwxy: null,
        yhjb: null,
        wxybh: "",
        wxyclr: "",
        wxyclrid: "",
        wxymc: "",
        sfnraqyx: null,
        wxyjb: null,
        yhbh: "",
        yhclr: "",
        yhclrid: "",
        yhmc: "",
      },
      processFormRules : {
        yzyj: [validateUtils.notNull],
      },
      formRules:{

      },
    });

    const loadWorkflow = () => {};
    onMounted(() => {
      loadWorkflow();
    });
    const { proxy } = getCurrentInstance() as any;
    const route = useRoute();
    const processHandleRef = ref();
    const taskRef = ref();
    const isValid = ref(true);
    const processFormRef = ref();
    const processExtFormRef = ref();
    /**
     * 流程操作变动事件
     */
     const handleOperateChanged = (operate: boolean)=>{
        isValid.value = operate;
    }

    const getData = (data: any) => {
      state.formData = data;
      state.jb = data?.report?.jb;
      state.processExtFormData.wxyjb = data?.report?.jb;
      state.processExtFormData.yhjb = data?.report?.jb;
      const user = useUserInfo().userInfos.user;
      state.processExtFormData.wxyclr = user.realname;
      state.processExtFormData.yhclr = user.realname;
    };

    //同意前操作
    const beforeShowFn = () => {
      state.submitLoading = true;
      const promise1 = state.jb == 20 ? processExtFormRef.value.validate() : null;
      const promise2 = processFormRef.value.validate();
      return Promise.all([promise1, promise2]).finally(()=>{state.submitLoading = false;});
    }
    
    //同意后操作
    const afterAgreeFn = (processHandle: any) => { 
      state.submitLoading = true;
      request.post("/voluntary/dept/deptHandle/valid", {
          ...state.formData.deptHandle,
          yzyj: state.formData.yzyj,
          processHandle: {
              processInsId: props.processInsId,
              taskId: props.taskId,
              ...processHandle,
          }
      }).then(()=>{
          mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
          ElMessage.success("办理完毕/Completed Processing");
      }).catch(()=>{
      }).finally(()=>{
          state.submitLoading = false;
      })
    }

    return {
      ...toRefs(state),
      ...toRefs(props),
      processHandleRef,
      taskRef,
      getData,
      isValid,
      handleOperateChanged,
      validateUtils,
      processFormRef,
      processExtFormRef,
      beforeShowFn,
      afterAgreeFn,
    };
  },
});
</script>